//
//  LGTeamMessageController.m
//  BreathTrainingTool
//
//  Created by 1234 on 2024/7/11.
//  编辑团队信息

#import "LGTeamMessageController.h"
#import "LGPersonalInputView.h"
@interface LGTeamMessageController ()<LGPersonalInputDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**   */
@property (nonatomic , strong)UILabel *introduceTitle;
/**   */
@property (nonatomic , strong)LGPersonalInputView *introduceView;
/**   */
@property (nonatomic , strong)UILabel *goodTitle;
/**   */
@property (nonatomic , strong)LGPersonalInputView *goodView;
/**   */
@property (nonatomic , strong)UIButton *saveBtn;
@end

@implementation LGTeamMessageController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    [self requestData];
    self.saveBtn.hidden = (self.canEdit == YES)?NO:YES;
    self.introduceView.userInteractionEnabled = (self.canEdit == YES)?YES:NO;
    self.goodView.userInteractionEnabled = (self.canEdit == YES)?YES:NO;
}

-(void)requestData{
    [RequestUtil withGET:@"/team/info" parameters:@{@"id":self.teamid} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.introduceView.content = LGNSString(returnData[@"name"]);
            self.goodView.content = LGNSString(returnData[@"description"]);
        }
    } error:^(NSError *error) {
        
    }];
}

//输入框高度变化
-(void)inputContentChanged:(CGFloat)height viewTag:(NSInteger)tag{
    LGPersonalInputView *inputView = [self.view viewWithTag:tag];
    [inputView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.equalTo(@(height+viewPix(55)));
        self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.saveBtn.frame)+viewPix(50));
    }];
    [self performSelector:@selector(resetContentSize) withObject:nil afterDelay:0.5];
}

-(void)resetContentSize{
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.saveBtn.frame)+viewPix(50));
}

//保存
-(void)saveBtnAction{
    [self.view endEditing:YES];
    [RequestUtil bodayPOST:@"/team/update" parameters:@{@"id":self.teamid,@"name":self.introduceView.content,@"description":self.goodView.content} header:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            [TooltipView showMessage:@"保存成功" offset:0];
            [self.navigationController popViewControllerAnimated:YES];
        }
    } error:^(NSError *error) {
        
    }];
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.title = @"团队信息";
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.introduceTitle];
    [self.scrollView addSubview:self.introduceView];
    [self.scrollView addSubview:self.goodTitle];
    [self.scrollView addSubview:self.goodView];
    [self.scrollView addSubview:self.saveBtn];
    [self.introduceView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.introduceTitle.mas_bottom).offset(viewPix(12));
        make.left.equalTo(self.view).offset(viewPix(16));
        make.right.equalTo(self.view).offset(-viewPix(16));
        make.height.equalTo(@(viewPix(65)));
    }];
    [self.goodTitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.introduceView.mas_bottom).offset(viewPix(21));
        make.left.right.equalTo(self.introduceView);
    }];
    [self.goodView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodTitle.mas_bottom).offset(viewPix(7));
        make.left.right.equalTo(self.introduceView);
        make.height.equalTo(@(viewPix(105)));
    }];
    [self.saveBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.goodView.mas_bottom).offset(viewPix(28));
        make.left.right.equalTo(self.introduceView);
        make.height.equalTo(@(viewPix(48)));
    }];
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
        _scrollView.backgroundColor = [UIColor colorWithString:@"#F9F9F9"];
        _scrollView.contentSize = CGSizeMake(Screen_W, Screen_H-topBarHeight+2);
    }
    return _scrollView;
}

-(UILabel *)introduceTitle{
    if(!_introduceTitle){
        _introduceTitle = [UILabel lableWithText:@"团队名称" colorString:@"#2B2A37" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _introduceTitle.frame = CGRectMake(viewPix(16), viewPix(16), Screen_W-viewPix(32), viewPix(23));
    }
    return _introduceTitle;
}


-(LGPersonalInputView *)introduceView{
    if(!_introduceView){
        _introduceView = [[LGPersonalInputView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.introduceTitle.frame)+viewPix(12), Screen_W-viewPix(32), viewPix(65))];
        _introduceView.contentTF.backgroundColor = [UIColor colorWithString:@"#F4F4F9"];
        _introduceView.backgroundColor = [UIColor colorWithString:@"#F4F4F9"];
        _introduceView.placeHolder = @"请输入团队名称";
        _introduceView.cornerRidus = 15;
        _introduceView.maxNumber = 30;
        _introduceView.delegate = self;
        _introduceView.tag = 1000;
    }
    return _introduceView;
}


-(UILabel *)goodTitle{
    if(!_goodTitle){
        _goodTitle = [UILabel lableWithText:@"团队介绍" colorString:@"#2B2A37" textFont:LGFontWeight(18, UIFontWeightMedium) textAlignment:NSTextAlignmentLeft lines:1];
        _goodTitle.frame = CGRectMake(viewPix(16), kMaxY(self.introduceView.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(23));
    }
    return _goodTitle;
}


-(LGPersonalInputView *)goodView{
    if(!_goodView){
        _goodView = [[LGPersonalInputView alloc] initWithFrame:CGRectMake(viewPix(16), kMaxY(self.goodTitle.frame)+viewPix(16), Screen_W-viewPix(32), viewPix(155))];
        _goodView.contentTF.backgroundColor = [UIColor colorWithString:@"#F4F4F9"];
        _goodView.backgroundColor = [UIColor colorWithString:@"#F4F4F9"];
        _goodView.placeHolder = @"请输入团队介绍";
        _goodView.cornerRidus = 15;
        _goodView.maxNumber = 300;
        _goodView.delegate = self;
        _goodView.tag = 2000;
    }
    return _goodView;
}


-(UIButton *)saveBtn{
    if(!_saveBtn){
        _saveBtn = [UIButton buttonWithTitle:@"保存" titleFont:LGFontWeight(16, UIFontWeightMedium) textColor:@"#FFFFFF" imageName:@""];
        _saveBtn.backgroundColor = [UIColor colorWithString:kThemeColor];
        _saveBtn.cornerRidus = 10;
        [_saveBtn addTarget:self action:@selector(saveBtnAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _saveBtn;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
